package fr.ants.controller.draganddrop;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JButton;

/**
 * This object represent data who save in the memory and used for the drag. Its
 * work is encapsulate the data and send the data at the destination with
 * checking if the data is supported.
 * 
 * if the target accept the flavor RecordFlavor,then we send the flavor
 * 
 * @author Guillaume Ruscassie
 * 
 */
public class ButtonTransferable implements Transferable {

	/**
	 * button.
	 */
	private JButton button;

	/**
	 * Record to transfer.
	 * 
	 * @param button
	 *            button
	 */
	protected ButtonTransferable(final JButton button) {
		this.button = button;
	}

	/*
	 * Make a list of flavor supported for the destination can be know if it can
	 * receive the data.
	 */
	@Override
	public DataFlavor[] getTransferDataFlavors() {
		DataFlavor[] data = { ButtonFlavorFactory.getUnitePanelFlavor() };
		return data;

	}

	/*
	 * Check the flavor supported.
	 */
	@Override
	public boolean isDataFlavorSupported(final DataFlavor flavor) {
		return ButtonFlavorFactory.getUnitePanelFlavor().equals(flavor)
				|| DataFlavor.stringFlavor.equals(flavor);
	}

	/*
	 * Get the data from the origin and send to the destination.
	 */
	@Override
	public Object getTransferData(final DataFlavor flavor)
			throws UnsupportedFlavorException, IOException {
		if (ButtonFlavorFactory.getUnitePanelFlavor().equals(flavor)) {
			return button;
		} else if (DataFlavor.stringFlavor.equals(flavor)) {
			return button;
		} else {
			throw new UnsupportedFlavorException(flavor);
		}
	}

}
